package com.simple.gateway.center.web;

import com.alibaba.fastjson.JSON;
import com.simple.gateway.center.basedata.common.ResponseCode;
import com.simple.gateway.center.basedata.common.Result;
import com.simple.gateway.center.domian.application.param.ApplicationInterfaceMethodParam;
import com.simple.gateway.center.domian.application.param.ApplicationInterfaceParam;
import com.simple.gateway.center.domian.application.param.ApplicationSystemParam;
import com.simple.gateway.center.application.IApplicationManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 项目: simple-gateway-core
 *
 * 功能描述:
 *
 * @author: WuChengXing
 *
 * @create: 2023-08-07 17:29
 **/
@RestController
@RequestMapping("/gateway/api/application")
@CrossOrigin
public class ApplicationManageController {

    private final Logger logger = LoggerFactory.getLogger(ApplicationManageController.class);

    @Resource
    private IApplicationManageService applicationManageService;

    @PostMapping(value = "/registerApplication", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerApplication(@RequestBody ApplicationSystemParam param) {
        try {
            logger.info("注册应用服务：{}", JSON.toJSONString(param));
            applicationManageService.registerApplication(param);
            return new Result<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), true);
        } catch (DuplicateKeyException e) {
            logger.warn("注册应用服务重复：{}", JSON.toJSONString(param), e);
            return new Result<>(ResponseCode.INDEX_DUP.getCode(), e.getMessage(), true);
        } catch (Exception e) {
            logger.error("注册应用服务失败：{}", JSON.toJSONString(param), e);
            return new Result<>(ResponseCode.UN_ERROR.getCode(), e.getMessage(), false);
        }
    }

    @PostMapping(value = "/registerApplicationInterface", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerApplicationInterface(@RequestBody ApplicationInterfaceParam param) {
        try {
            logger.info("注册应用接口：{}", JSON.toJSONString(param));
            applicationManageService.registerApplicationInterface(param);
            return new Result<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), true);
        } catch (DuplicateKeyException e) {
            logger.warn("注册应用接口重复：{}", JSON.toJSONString(param));
            return new Result<>(ResponseCode.INDEX_DUP.getCode(), e.getMessage(), true);
        } catch (Exception e) {
            logger.error("注册应用接口失败：{}", JSON.toJSONString(param), e);
            return new Result<>(ResponseCode.UN_ERROR.getCode(), e.getMessage(), false);
        }
    }

    @PostMapping(value = "/registerApplicationInterfaceMethod", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerApplicationInterfaceMethod(@RequestBody ApplicationInterfaceMethodParam param) {
        try {
            logger.info("注册应用接口方法：{}", JSON.toJSONString(param));
            applicationManageService.registerApplicationInterfaceMethod(param);
            return new Result<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), true);
        } catch (DuplicateKeyException e) {
            logger.warn("注册应用接口重复：{}", JSON.toJSONString(param));
            return new Result<>(ResponseCode.INDEX_DUP.getCode(), e.getMessage(), true);
        } catch (Exception e) {
            logger.error("注册应用接口失败：{}", JSON.toJSONString(param), e);
            return new Result<>(ResponseCode.UN_ERROR.getCode(), e.getMessage(), false);
        }
    }

    @PostMapping(value = "/updateApplicationInterface", produces = "application/json;charset=utf-8")
    public Result<Boolean> updateApplicationInterface(@RequestBody ApplicationInterfaceParam param) {
        return new Result<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), true);
    }
}
